Skip to content

[ONNX] Add eliminate_nop_cast pass #3376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from

Conversation

andrey-churkin
Copy link
Contributor

Changes

Apply the eliminate_nop_cast optimization pass from onnxoptimizer before building the NNCF graph. This pass aims to eliminate no-op cast nodes in the graph. A no-op cast is a cast that doesn't change the value, such as casting a tensor to its own type.

Reason for changes

Statistics cannot be collected after the no-op Cast nodes because such nodes are removed from the ONNX inference graph during the session.

Related tickets

Ref: 164211

@andrey-churkin andrey-churkin requested a review from a team as a code owner March 25, 2025 09:30
@github-actions github-actions bot added the NNCF ONNX Pull requests that updates NNCF ONNX label Mar 25, 2025
@@ -348,6 +349,7 @@ def create_nncf_graph(onnx_model: onnx.ModelProto) -> NNCFGraph:
"""
onnx_model = GraphConverter._replace_empty_node_name(onnx_model)
onnx_model = onnx.shape_inference.infer_shapes(onnx_model)
onnx_model = onnxoptimizer.optimize(onnx_model, ["eliminate_nop_cast"])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other passes that can be useful as well.

"eliminate_nop_dropout",
"eliminate_nop_flatten",
"eliminate_if_with_const_cond",
"eliminate_nop_monotone_argmax",
"eliminate_nop_pad",
"eliminate_nop_concat",
"eliminate_nop_split",
"eliminate_nop_expand",
"eliminate_shape_gather",
"eliminate_slice_after_shape",
"eliminate_nop_transpose",
"eliminate_nop_reshape",
"eliminate_nop_with_unit",
"eliminate_common_subexpression",
"eliminate_deadend",
"eliminate_identity",
"eliminate_shape_op",
"eliminate_unused_initializer",
"eliminate_duplicate_initializer",

Comment on lines 351 to 352
onnx_model = onnx.shape_inference.infer_shapes(onnx_model)
onnx_model = onnxoptimizer.optimize(onnx_model, ["eliminate_nop_cast"])
Copy link
Contributor Author

@andrey-churkin andrey-churkin Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pass should be applied after onnx.shape_inference.infer_shapes() call. Otherwise, not all no-op Cast nodes will be found.

@@ -348,6 +349,7 @@ def create_nncf_graph(onnx_model: onnx.ModelProto) -> NNCFGraph:
"""
onnx_model = GraphConverter._replace_empty_node_name(onnx_model)
onnx_model = onnx.shape_inference.infer_shapes(onnx_model)
onnx_model = onnxoptimizer.optimize(onnx_model, ["eliminate_nop_cast"])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following code snippet can be used to apply that pass to the initial ONNX model.

import onnx
import onnxoptimizer

model = onnx.load(MODEL_PATH)
model = onnx.shape_inference.infer_shapes(model)
model = onnxoptimizer.optimize(model, ["eliminate_nop_cast"])

@andrey-churkin
Copy link
Contributor Author

@kshpv Please review

@andrey-churkin andrey-churkin force-pushed the ac/eliminate_nop_cast branch from 4650dc4 to fa04951 Compare April 3, 2025 18:20
@andrey-churkin andrey-churkin requested a review from alexsu52 April 3, 2025 18:21
@alexsu52 alexsu52 removed the request for review from kshpv April 20, 2025 17:06
@alexsu52 alexsu52 self-assigned this Apr 20, 2025
Copy link
Contributor

@alexsu52 alexsu52 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this PR in draft or can it be reviewed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NNCF ONNX Pull requests that updates NNCF ONNX
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants